/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test_system.menu;

import java.util.Scanner;
import test_system.questions.TestWrapper;

/**
 *
 * @author root
 */
public class Menu {

    private TestWrapper test;
    private Scanner in = new Scanner(System.in);
    Language language;

    public Menu(TestWrapper test) {
        this.test = test;
    }
    
    public String[] runTestDialog(){
        checkLanguage();
        String[] result = polling();
        return result;
    }
    
    public void markOutput(boolean[] userResult){
        int mark = 0;
        for(int i = 0; i < userResult.length; i++){
            if(userResult[i]){
                mark++;
            }
        }
        String markOutout = "%1$s %2$d/%3$d";
        System.out.println(String.format(markOutout, language.markOutout(), mark, userResult.length));
    }
    

    private String[] polling(){
        String[] result = new String[test.getTestLength()];
        for(int i = 0; i < test.getTestLength(); i++){
            outputQuestion(i);
            result[i] = in.nextLine();
        }
        return result;
        
    }



    private void outputQuestion(int index) {
        String[] languageContext = language.getQuestionContext();
        String fullQuestion = "%1$s\n%2$s";
        if (test.getQuestion(index).getClassName().equals("TextAnswerQuestion")) {
            System.out.println(String.format(fullQuestion, languageContext[0], test.getQuestion(index).toString()));
        } else {
            System.out.println(String.format(fullQuestion, languageContext[1], test.getQuestion(index).toString()));
        }

    }

    private void checkLanguage() {
        String lan;
        do {
            System.out.println("Please, check your language:\n1)English\n2)Українська\n3)Русский");
            lan = in.nextLine();
            if (lan.equals("1")) {
                language = new EnLanguage();
            } else if (lan.equals("2")) {
                language = new UaLanguage();
            } else if (lan.equals("3")) {
                language = new RuLanguage();
            } else {
                System.out.println("Check right answer.");
            }
        } while (!(lan.equals("1") || lan.equals("2") || lan.equals("3")));
    }
}
